//
//  HLFiltrateView.m
//  zhuanxian
//
//  Created by zhaoliang chen on 2017/11/19.
//  Copyright © 2017年 huiling. All rights reserved.
//

#import "HLFiltrateView.h"
#import "HLUnderlineView.h"
#import "ActionSheetPicker.h"

@interface HLFiltrateView()
<UITextFieldDelegate>

@property(nonatomic,strong)HLWaybillFiltrateObject* filtrateObject;
@property(nonatomic,strong)UITextField* textFieldStartDate;
@property(nonatomic,strong)UITextField* textFieldEndDate;
@property(nonatomic,strong)UITextField* textFieldDeliverType;
@property(nonatomic,strong)UITextField* textFieldPayType;
@property(nonatomic,strong)UITextField* textFieldBillState;
@property(nonatomic,strong)UITextField* textFieldRecvStation;

@end

@implementation HLFiltrateView

@synthesize onCancelFilter;
@synthesize onConfirmFilter;

#define rowHeight   60
#define leftInset   5

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.filtrateObject = [[HLWaybillFiltrateObject alloc]init];
        
        HLUnderlineView* viewStartDate = [[HLUnderlineView alloc]init];
        [self addSubview:viewStartDate];
        [viewStartDate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(self);
            make.height.mas_equalTo(rowHeight);
        }];
        
        UILabel* labelStartDate = [UILabel new];
        labelStartDate.text = @"开始时间";
        [viewStartDate addSubview:labelStartDate];
        [labelStartDate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewStartDate);
            make.width.mas_equalTo(70);
            make.left.mas_equalTo(leftInset);
        }];
        
        [viewStartDate addSubview:self.textFieldStartDate];
        [self.textFieldStartDate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewStartDate);
            make.right.mas_equalTo(-5);
            make.left.mas_equalTo(labelStartDate.mas_right).offset(10);
            make.height.mas_equalTo(viewStartDate);
        }];
        
        //结束时间
        HLUnderlineView* viewEndDate = [[HLUnderlineView alloc]init];
        [self addSubview:viewEndDate];
        [viewEndDate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.top.mas_equalTo(viewStartDate.mas_bottom);
            make.height.mas_equalTo(rowHeight);
        }];
        
        UILabel* labelEndDate = [UILabel new];
        labelEndDate.text = @"结束时间";
        [viewEndDate addSubview:labelEndDate];
        [labelEndDate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewEndDate);
            make.width.mas_equalTo(70);
            make.left.mas_equalTo(leftInset);
        }];
        
        [viewEndDate addSubview:self.textFieldEndDate];
        [self.textFieldEndDate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewEndDate);
            make.right.mas_equalTo(-5);
            make.left.mas_equalTo(labelEndDate.mas_right).offset(10);
            make.height.mas_equalTo(viewEndDate);
        }];
        
        //提货方式
        HLUnderlineView* viewDeliverType = [[HLUnderlineView alloc]init];
        [self addSubview:viewDeliverType];
        [viewDeliverType mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.top.mas_equalTo(viewEndDate.mas_bottom);
            make.height.mas_equalTo(rowHeight);
        }];
        
        UILabel* labelDeliverType = [UILabel new];
        labelDeliverType.text = @"提货方式";
        [viewDeliverType addSubview:labelDeliverType];
        [labelDeliverType mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewDeliverType);
            make.width.mas_equalTo(70);
            make.left.mas_equalTo(leftInset);
        }];
        
        [viewDeliverType addSubview:self.textFieldDeliverType];
        [self.textFieldDeliverType mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewDeliverType);
            make.right.mas_equalTo(-5);
            make.left.mas_equalTo(labelDeliverType.mas_right).offset(10);
            make.height.mas_equalTo(viewDeliverType);
        }];
        
        //付款方式
        HLUnderlineView* viewPayType = [[HLUnderlineView alloc]init];
        [self addSubview:viewPayType];
        [viewPayType mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.top.mas_equalTo(viewDeliverType.mas_bottom);
            make.height.mas_equalTo(rowHeight);
        }];
        
        UILabel* labelPayType = [UILabel new];
        labelPayType.text = @"付款方式";
        [viewPayType addSubview:labelPayType];
        [labelPayType mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewPayType);
            make.width.mas_equalTo(70);
            make.left.mas_equalTo(leftInset);
        }];
        
        [viewPayType addSubview:self.textFieldPayType];
        [self.textFieldPayType mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewPayType);
            make.right.mas_equalTo(-5);
            make.left.mas_equalTo(labelPayType.mas_right).offset(10);
            make.height.mas_equalTo(viewPayType);
        }];
        
        //运单状态
        HLUnderlineView* viewBillState = [[HLUnderlineView alloc]init];
        [self addSubview:viewBillState];
        [viewBillState mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.top.mas_equalTo(viewPayType.mas_bottom);
            make.height.mas_equalTo(rowHeight);
        }];
        
        UILabel* labelBillState = [UILabel new];
        labelBillState.text = @"运单状态";
        [viewBillState addSubview:labelBillState];
        [labelBillState mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewBillState);
            make.width.mas_equalTo(70);
            make.left.mas_equalTo(leftInset);
        }];
        
        [viewBillState addSubview:self.textFieldBillState];
        [self.textFieldBillState mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewBillState);
            make.right.mas_equalTo(-5);
            make.left.mas_equalTo(labelBillState.mas_right).offset(10);
            make.height.mas_equalTo(viewBillState);
        }];
        
        //运单状态
        HLUnderlineView* viewRecvStation = [[HLUnderlineView alloc]init];
        [self addSubview:viewRecvStation];
        [viewRecvStation mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.top.mas_equalTo(viewBillState.mas_bottom);
            make.height.mas_equalTo(rowHeight);
        }];
        
        UILabel* labelRecvStation = [UILabel new];
        labelRecvStation.text = @"网点";
        [viewRecvStation addSubview:labelRecvStation];
        [labelRecvStation mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewRecvStation);
            make.width.mas_equalTo(40);
            make.left.mas_equalTo(leftInset);
        }];
        
        [viewRecvStation addSubview:self.textFieldRecvStation];
        [self.textFieldRecvStation mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(viewRecvStation);
            make.right.mas_equalTo(-5);
            make.left.mas_equalTo(labelRecvStation.mas_right).offset(10);
            make.height.mas_equalTo(viewRecvStation);
        }];
        
        UIButton* btConfirm = [UIButton buttonWithType:UIButtonTypeCustom];
        [btConfirm setTitle:@"确定" forState:UIControlStateNormal];
        btConfirm.tag = 1;
        [btConfirm setTitleColor:MainColor forState:UIControlStateNormal];
        [btConfirm addTarget:self action:@selector(onClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btConfirm];
        [btConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(viewRecvStation.mas_bottom).offset(10);
            make.right.mas_equalTo(-10);
        }];
        
        UIButton* btCancel = [UIButton buttonWithType:UIButtonTypeCustom];
        [btCancel setTitle:@"取消" forState:UIControlStateNormal];
        btCancel.tag = 0;
        [btCancel setTitleColor:MainColor forState:UIControlStateNormal];
        [btCancel addTarget:self action:@selector(onClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btCancel];
        [btCancel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(viewRecvStation.mas_bottom).offset(10);
            make.right.mas_equalTo(btConfirm.mas_left).offset(-20);
        }];
    }
    return self;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    WeakSelf;
    if (textField == self.textFieldStartDate) {
        ActionSheetDatePicker *actionSheetDatePicker = [[ActionSheetDatePicker alloc] initWithTitle:@"请选择开始时间" datePickerMode:UIDatePickerModeDate selectedDate:[NSDate date] doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {
            weakSelf.textFieldStartDate.text = formatDateToString(selectedDate);
            weakSelf.filtrateObject.startCreateTime = weakSelf.textFieldStartDate.text;
        } cancelBlock:nil origin:self];
        [actionSheetDatePicker showActionSheetPicker];
    } else if (textField == self.textFieldEndDate) {
        ActionSheetDatePicker *actionSheetDatePicker = [[ActionSheetDatePicker alloc] initWithTitle:@"请选择结束日期时间" datePickerMode:UIDatePickerModeDate selectedDate:[NSDate date] doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {
            weakSelf.textFieldEndDate.text = formatDateToString(selectedDate);
            weakSelf.filtrateObject.endCreateTime = weakSelf.textFieldEndDate.text;
        } cancelBlock:nil origin:self];
        [actionSheetDatePicker showActionSheetPicker];
    } else if (textField == self.textFieldDeliverType) {
        [ActionSheetStringPicker showPickerWithTitle:@"提送方式" rows:@[@"自提自装",@"送货",@"签回单",@"自提装站",@"送货到仓",@"代提代送",@"代提自提",@"车辆接驳"] initialSelection:0 doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
            weakSelf.textFieldDeliverType.text = selectedValue;
            weakSelf.filtrateObject.service = weakSelf.textFieldDeliverType.text;
        } cancelBlock:^(ActionSheetStringPicker *picker) {
            
        } origin:self];
    } else if (textField == self.textFieldPayType) {
        [ActionSheetStringPicker showPickerWithTitle:@"付款方式" rows:@[@"提付",@"现付",@"回付",@"发货月结",@"收货月结",@"多项付费"] initialSelection:0 doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
            weakSelf.textFieldPayType.text = selectedValue;
            weakSelf.filtrateObject.paymentWay = weakSelf.textFieldPayType.text;
        } cancelBlock:^(ActionSheetStringPicker *picker) {
            
        } origin:self];
    } else if (textField == self.textFieldBillState) {
        [ActionSheetStringPicker showPickerWithTitle:@"运单状态" rows:@[@"新建",@"运输中",@"到站",@"欠费",@"签收"] initialSelection:0 doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
            weakSelf.textFieldBillState.text = selectedValue;
            weakSelf.filtrateObject.status = weakSelf.textFieldBillState.text;
        } cancelBlock:^(ActionSheetStringPicker *picker) {
            
        } origin:self];
    } else if (textField == self.textFieldRecvStation) {
        [ActionSheetStringPicker showPickerWithTitle:@"到站网点" rows:@[@"无网点",@"北京网点",@"成都网点",@"重庆网点"] initialSelection:0 doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
            weakSelf.textFieldRecvStation.text = selectedValue;
            weakSelf.filtrateObject.destinationWebsiteId = weakSelf.textFieldRecvStation.text;
        } cancelBlock:^(ActionSheetStringPicker *picker) {
            
        } origin:self];
    }
    return NO;
}

- (void)onClickButton:(UIButton*)btn {
    if (btn.tag == 0) {
        if (onCancelFilter) {
            onCancelFilter();
        }
    } else {
        if (onConfirmFilter) {
            onConfirmFilter(self.filtrateObject);
        }
    }
}

- (UITextField*)textFieldStartDate {
    if (!_textFieldStartDate) {
        _textFieldStartDate = [UITextField new];
        _textFieldStartDate.delegate = self;
        _textFieldStartDate.placeholder = @"请选择开始日期";
        _textFieldStartDate.textColor = MainColor;
        _textFieldStartDate.textAlignment = NSTextAlignmentRight;
        UIImageView* imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hl_date_choose"]];
        imgV.frame = CGRectMake(0, 0, 40, 20);
        imgV.contentMode = UIViewContentModeCenter;
        _textFieldStartDate.rightView = imgV;
        _textFieldStartDate.rightViewMode = UITextFieldViewModeAlways;
    }
    return _textFieldStartDate;
}

- (UITextField*)textFieldEndDate {
    if (!_textFieldEndDate) {
        _textFieldEndDate = [UITextField new];
        _textFieldEndDate.delegate = self;
        _textFieldEndDate.placeholder = @"请选择结束日期";
        _textFieldEndDate.textColor = MainColor;
        _textFieldEndDate.textAlignment = NSTextAlignmentRight;
        UIImageView* imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hl_date_choose"]];
        imgV.frame = CGRectMake(0, 0, 40, 20);
        imgV.contentMode = UIViewContentModeCenter;
        _textFieldEndDate.rightView = imgV;
        _textFieldEndDate.rightViewMode = UITextFieldViewModeAlways;
    }
    return _textFieldEndDate;
}

- (UITextField*)textFieldDeliverType {
    if (!_textFieldDeliverType) {
        _textFieldDeliverType = [UITextField new];
        _textFieldDeliverType.delegate = self;
        _textFieldDeliverType.placeholder = @"请选择提送方式";
        _textFieldDeliverType.textAlignment = NSTextAlignmentRight;
        _textFieldDeliverType.textColor = MainColor;
        UIImageView* imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hl_arrow_down_orange"]];
        imgV.frame = CGRectMake(0, 0, 40, 20);
        imgV.contentMode = UIViewContentModeCenter;
        _textFieldDeliverType.rightView = imgV;
        _textFieldDeliverType.rightViewMode = UITextFieldViewModeAlways;
    }
    return _textFieldDeliverType;
}

- (UITextField*)textFieldPayType {
    if (!_textFieldPayType) {
        _textFieldPayType = [UITextField new];
        _textFieldPayType.delegate = self;
        _textFieldPayType.placeholder = @"请选择付款方式";
        _textFieldPayType.textAlignment = NSTextAlignmentRight;
        _textFieldPayType.textColor = MainColor;
        UIImageView* imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hl_arrow_down_orange"]];
        imgV.frame = CGRectMake(0, 0, 40, 20);
        imgV.contentMode = UIViewContentModeCenter;
        _textFieldPayType.rightView = imgV;
        _textFieldPayType.rightViewMode = UITextFieldViewModeAlways;
    }
    return _textFieldPayType;
}

- (UITextField*)textFieldBillState {
    if (!_textFieldBillState) {
        _textFieldBillState = [UITextField new];
        _textFieldBillState.delegate = self;
        _textFieldBillState.placeholder = @"请选择状态";
        _textFieldBillState.textAlignment = NSTextAlignmentRight;
        _textFieldBillState.textColor = MainColor;
        UIImageView* imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hl_arrow_down_orange"]];
        imgV.frame = CGRectMake(0, 0, 40, 20);
        imgV.contentMode = UIViewContentModeCenter;
        _textFieldBillState.rightView = imgV;
        _textFieldBillState.rightViewMode = UITextFieldViewModeAlways;
    }
    return _textFieldBillState;
}

- (UITextField*)textFieldRecvStation {
    if (!_textFieldRecvStation) {
        _textFieldRecvStation = [UITextField new];
        _textFieldRecvStation.delegate = self;
        _textFieldRecvStation.placeholder = @"请选择网点";
        _textFieldRecvStation.textAlignment = NSTextAlignmentRight;
        _textFieldRecvStation.textColor = MainColor;
        UIImageView* imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hl_arrow_down_orange"]];
        imgV.frame = CGRectMake(0, 0, 40, 20);
        imgV.contentMode = UIViewContentModeCenter;
        _textFieldRecvStation.rightView = imgV;
        _textFieldRecvStation.rightViewMode = UITextFieldViewModeAlways;
    }
    return _textFieldRecvStation;
}

@end
